package com.example.presentshopping.ui.fragment.earnings

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.GiftMoneyFragmentBean
import com.example.presentshopping.config.bean.SpecialOrderBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.utils.tool.GsonUtils
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlinx.android.synthetic.main.giftmoney_fragment.*

class GiftMoneyFragment(override var context: Context, var str: String) :BaseMvpFragment(){

    var   finMoneyFragmentBean : GiftMoneyFragmentBean?=null

    private var sendMesPresenter: PublicPresenter? = null
    var refreshlayout: SwipeRefreshLayout? = null
   var recycler_view : SwipeRecyclerView ?=null

    var baseRecyclerAdapter: BaseRecyclerAdapter<GiftMoneyFragmentBean.DataBean.ListBean>? = null

    var page =1

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        sendMesPresenter = PublicPresenter(context,false,"")
        sendMesPresenter?.attachView(this)

        recycler_view =view?.findViewById(R.id.recycler_view)
        refreshlayout = view?.findViewById(R.id.refresh_layout)


        setAdapter()
        OnsInit()
        recycler_view?.useDefaultLoadMore()
        recycler_view?.loadMoreFinish(false, true)
        refreshlayout?.setOnRefreshListener {
            page = 1
            OnsInit()
        }
        recycler_view?.setLoadMoreListener {
            page++
            OnsInit()
        }


    }


    fun setAdapter(){
        baseRecyclerAdapter = object :BaseRecyclerAdapter<GiftMoneyFragmentBean.DataBean.ListBean>(context ,null,R.layout.item_finmoney_fragmen){
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: GiftMoneyFragmentBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val from_type = holder?.getView<TextView>(R.id.from_type)
                val type = holder?.getView<TextView>(R.id.type)
                val total = holder?.getView<TextView>(R.id.total)
                val order_id = holder?.getView<TextView>(R.id.order_id)

                type?.text =data.create_time

                if (data.status==1){
                    order_id?.text = "订单编号: "+data.order_id
                    total?.text ="+ "+data.total
                    total?.setTextColor(Color.parseColor("#F11801"))
                }else{
                    order_id?.text ="支出"
                    total?.text ="- "+data.total
                    total?.setTextColor(Color.parseColor("#48C960"))
                }
            }

        }

        recycler_view?.adapter =  baseRecyclerAdapter
    }

    fun OnsInit(){
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_getBlockCurrencyChange,
            HttpRequest.getTodatearnin(str,page,10))
    }

    override fun onDestroyView() {
        sendMesPresenter?.detachView()
        super.onDestroyView()
    }
    override fun onSuccess(data: String?) {
        finMoneyFragmentBean = GsonUtils.getGson().fromJson(data,GiftMoneyFragmentBean().javaClass)
        refreshlayout?.isRefreshing = false
        if (page == 1) {
            baseRecyclerAdapter?.clear()
        }
        if(finMoneyFragmentBean?.data?.list?.size!!>0){
            recycler_view?.loadMoreFinish(false,true)
            baseRecyclerAdapter?.setData(finMoneyFragmentBean?.data?.list)
        }else{
            recycler_view?.loadMoreFinish(true,false)
        }


    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    override val layoutId: Int
        get() = R.layout.giftmoney_fragment
}